#include <sstream>
#include "TileCutter.hpp"
#include "Tile.hpp"

TileCutter::TileCutter(std::list<SpriteSheet*>& imageList):
    _imageList(imageList)
{
}

void            TileCutter::cutSprite(QString const& name, QString const& path, const int spriteWidth, const int spriteHeight)
{
    SpriteSheet*    sheet = new SpriteSheet(name, path, spriteWidth, spriteHeight);
    QPixmap         spriteSheet(path);

    int             imageWidth = spriteSheet.width(), imageHeight = spriteSheet.height();
    int             x, y;

    std::stringstream   nameFormater;
    int                 itSprite = 1;
    std::string         stdName = name.toStdString();

    y = 0;
    while (y < imageHeight)
    {
        x = 0;
        while (x < imageWidth)
        {
            nameFormater << stdName << " - " << itSprite;
            sheet->addSprite(nameFormater.str().data(), spriteSheet.copy(x, y, spriteWidth, spriteHeight).scaled(TILE_SIZE, TILE_SIZE));
            nameFormater.str("");
            itSprite++;

            x += spriteWidth;
        }
        y += spriteHeight;
    }

    _imageList.push_back(sheet);
}
